package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.UserBanquetMember;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBanquetMemberDaoImpl extends TempDataObjectDao<UserBanquetMember> {

    public Map<Long, UserBanquetMember> getUserBanquetMemberMap(String banquetId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_banquet_member where banquetId = ?";
        Map<Long, UserBanquetMember> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++,banquetId);

            rs = call.executeQuery();
            while (rs.next()) {
                UserBanquetMember data = parse(rs);
                res.put(data.getUserId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return res;
    }


    @Override
    protected boolean addData(UserBanquetMember data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_banquet_member(banquetId,userId," +
                "robot,robotProcessId,robotId,giftId,popularity,integral,pos,userBaseInfo)" +
                " values(?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getBanquetId());
            call.setLong(i++, data.getUserId());
            call.setBoolean(i++, data.isRobot());
            call.setInt(i++, data.getRobotProcessId());
            call.setInt(i++, data.getRobotId());
            call.setLong(i++, data.getGiftId());
            call.setLong(i++, data.getPopularity());
            call.setLong(i++, data.getIntegral());
            call.setLong(i++, data.getPos());
            call.setString(i++, data.getUserBaseInfoJsonStr());

            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserBanquetMember data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_banquet_member SET popularity=?,integral=? where banquetId=? and userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getPopularity());
            call.setLong(i++, data.getIntegral());
            call.setString(i++, data.getBanquetId());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserBanquetMember parseData(ResultSet rs) throws SQLException {
        UserBanquetMember data = new UserBanquetMember();
        data.setBanquetId(rs.getString("banquetId"));
        data.setUserId(rs.getLong("userId"));
        data.setRobot(rs.getBoolean("robot"));
        data.setRobotProcessId(rs.getInt("robotProcessId"));
        data.setRobotId(rs.getInt("robotId"));
        data.setGiftId(rs.getInt("giftId"));
        data.setPopularity(rs.getLong("popularity"));
        data.setIntegral(rs.getLong("integral"));
        data.setPos(rs.getInt("pos"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setUserBaseInfo(userBaseInfo);
        }
        return data;
    }
}
